/**
 * 
 */
package com.pcxt.net;

import java.io.IOException;
import java.util.HashMap;
import java.util.Vector;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;

import android.util.Log;

import com.pcxt.exception.XException;
import com.pcxt.xml.*;

/**
 * @author linchiachien
 *
 */
public class HttpConnectionDelegate implements IHttpReceiver {
	
	private IDataCallBack parent = null;
	private String username;
	private String password;
	private String processorname;
	private HttpConnectionProcessor connectionProcessor = null;
	private XMLProcessor xmlProcessor = null;
	
	public static HttpConnectionDelegate initialize(IDataCallBack parent, String username, 
			String password, String processorname) {
		HttpConnectionDelegate result = new HttpConnectionDelegate();
			
		result.parent = parent;
		result.username = username;
		result.password = password;
		result.processorname = processorname;
		
		return result;
	}
	
	
	public void connect(String obid) throws XException{
		connectionProcessor = HttpConnectionProcessor.initialize(this);
		
		connectionProcessor.getRemoteFileContents(obid);
	}
	
	
	/*
	 * call initialize
	 */
	private HttpConnectionDelegate() {
		
	}

	/* (non-Javadoc)
	 * @see com.pcxt.net.IHttpReceiver#refreshData(org.apache.http.HttpResponse)
	 */
	public void refreshData(HttpResponse response) {
		// TODO Auto-generated method stub
		Vector<HashMap<String,Object>> result = null;
        try {

        	xmlProcessor = XMLProcessor.initialize(response.getEntity().getContent());
        	result = xmlProcessor.getInstances();

        	
        	parent.received(result);
        	
        }
        catch (XException xe) {
        	Log.e("XMLProcessor",xe.getMessage());
        }
        catch (IOException ioe) {
        }
		Log.d("onNetwork", "Response received.");
	}

	/* (non-Javadoc)
	 * @see com.pcxt.net.IHttpReceiver#getParams(java.lang.String)
	 */
	public HttpPost getParams(String obid) {
		// TODO Auto-generated method stub
		return parent.getParams(obid);
	}

	/* (non-Javadoc)
	 * @see com.pcxt.net.IHttpReceiver#getUserid()
	 */
	public String getUserid() {
		// TODO Auto-generated method stub
		return username;
	}

	/* (non-Javadoc)
	 * @see com.pcxt.net.IHttpReceiver#getPassword()
	 */
	public String getPassword() {
		// TODO Auto-generated method stub
		return password;
	}

	/* (non-Javadoc)
	 * @see com.pcxt.net.IHttpReceiver#getProcessorName()
	 */
	public String getProcessorName() {
		// TODO Auto-generated method stub
		return processorname;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
